﻿using Dispatch.Model;
using Dispatch.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Dispatch.DispatchData
{

    internal class MarkerDataSet
    {
        public static List<MarkerModel> markerList = new List<MarkerModel>(); //所有的地标列表
        public static List<StayMarker> stayList = new List<StayMarker>(); //待命点列表


        #region 初始化数据

        public static void FillData(List<MarkerModel> list)
        {
            lock (markerList)
            {
                markerList.Clear();
                markerList.AddRange(list);
            }
        }


        public static void FillStayData(List<StayMarker> list)
        {
            lock (stayList)
            {
                stayList.Clear();
                stayList.AddRange(list);
            }
        }

        #endregion


        /// <summary>
        /// 获取点位，因为没有修改，所以不用上锁
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        public static  MarkerModel?  GetMarkerByPos(int x, int y)
        {
            MarkerModel? marker = null;
            var tmp = markerList.Where(m => Math.Abs(m.X - x) <= 30 && Math.Abs(m.Y - y) <= 30).FirstOrDefault();
            if (tmp != null)
            {
                marker = Tools.Copy(tmp);
            }
            return marker;
        }

        /// <summary>
        /// 获取某个工序停得最久的
        /// </summary>
        /// <param name="Process"></param>
        /// <returns></returns>
        public static Task<StayMarker?> GetAgvByProcess(string Process)
        {
            var tmp = stayList.Where(m => m.Process == Process && m.AgvID > 0).OrderByDescending(x => x.LockTime).FirstOrDefault();
            StayMarker? obj = null;
            if (tmp != null)
            {
                obj = Tools.Copy(tmp);
            }
            return Task.FromResult(obj);
        }



        /// <summary>
        /// 获取可用的待命位
        /// </summary>
        /// <param name="Process"></param>
        /// <returns></returns>
        public static Task<StayMarker?> GetEmptyStayMarker(string Process)
        {
            var tmp = stayList.Where(m => m.Process == Process && m.AgvID == 0).FirstOrDefault();
            StayMarker? obj = null;
            if (tmp != null)
            {
                obj = Tools.Copy(tmp);
            }
            return Task.FromResult(obj);
        }


        /// <summary>
        /// 锁定待命点
        /// </summary>
        /// <param name="MarkerID"></param>
        /// <param name="AgvID"></param>
        /// <returns></returns>
        public static Task<bool> LockStayMarker(string MarkerID, int AgvID)
        {
            lock (stayList)
            {
                var tmp = stayList.Find(m => m.MarkerID == MarkerID);
                if (tmp != null)
                {
                    if (tmp.AgvID > 0 && tmp.AgvID != AgvID)
                    {
                        //被其它AGV锁了
                        return Task.FromResult(false);
                    }
                    else
                    {
                        tmp.LockTime = DateTime.Now;
                        tmp.AgvID = AgvID;

                        _=ToCacheAsync(tmp);

                        return Task.FromResult(true);
                    }
                }
                else
                {
                    return Task.FromResult(false);
                }
            }
        }

        /// <summary>
        /// 获取坞前地标对应的传输坞地标编号(自己定义规则） 
        /// </summary>
        /// <param name="BeforeMarkerID"></param>
        /// <returns></returns>
        public static string GetTransMarker(string BeforeMarkerID)
        {
            return BeforeMarkerID +"1";
        }


        /// <summary>
        /// 获取传输坞对应的坞前地标编号(自己定义规则）
        /// </summary>
        /// <param name="TransMarkerID"></param>
        /// <returns></returns>
        public static string GetBeforeMarker(string TransMarkerID)
        {
            return TransMarkerID + "2";
        }


        #region 写缓存

       static Task ToCacheAsync(StayMarker obj)
        {
            return Task.CompletedTask;
        }


        #endregion

    }

    /// <summary>
    /// 待命点,存在修改，不能struct
    /// </summary>
    public class StayMarker
    { 
        public string Process { get; set; } = "";
        public string MarkerID { get; set; } = "";
        public int AgvID { get; set; }
        public DateTime? LockTime { get; set; }
    }

    public enum MarkerEnum
    {
        待命=1, 
        传输坞选择=2, 
        充电=3, 
        传输坞=4,
        坞前=5,
    }
}
